package com.jueling.culture.controller;

import com.jueling.culture.core.Result;
import com.jueling.culture.core.ResultGenerator;
import com.jueling.culture.model.AgentConfig;
import com.jueling.culture.service.AgentConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;

/**
* @describe 代理配置
* @author linp
* @date 2022/04/11
*/
@RestController
public class AgentConfigController {
    @Autowired
    private AgentConfigService agentConfigService;

    @PostMapping("/api/agent/config")
    public Result add(@RequestBody AgentConfig agentConfig) {
        //获取最大代理级别ID
        String dljdId = agentConfigService.getMaxDljbId();
        Integer num = Integer.valueOf(dljdId.substring(4));
        DecimalFormat decimalFormat = new DecimalFormat("0000");
        num++;
        String dljbId = "jlwh" +  decimalFormat .format(num);
        //判断是否已存在
        while(agentConfigService.getDljbId(dljbId)){
            num++;
        }
        agentConfig.setDljbId("jlwh" +  decimalFormat .format(num));
        agentConfigService.save(agentConfig);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/api/agent/config/{id}")
    public Result delete(@PathVariable String id) {
        agentConfigService.deleteAgentConfig(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping("/api/agent/config")
    public Result update(@RequestBody AgentConfig agentConfig) {
        agentConfigService.update(agentConfig);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/api/agent/config/{id}")
    public Result detail(@PathVariable String id) {
        AgentConfig agentConfig = agentConfigService.getAgentConfig(id);
        return ResultGenerator.genSuccessResult(agentConfig);
    }

    @GetMapping("/api/agent/config")
    public Result list() {
        return ResultGenerator.genSuccessResult(agentConfigService.findAll());
    }

    /**
     * 下拉框使用
     * @return
     */
    @GetMapping("/api/agent/config/list")
    public Result listSelect() {
        return ResultGenerator.genSuccessResult(agentConfigService.listSelect());
    }





}
